package com.koron.common.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.koron.bean.base.Response;
import com.koron.bean.query.Page;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.SubmitQuery;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.business.workflow.bean.dto.WorkflowStaff;
import com.koron.common.core.business.workflow.bean.param.HandlerParams;
import com.koron.common.core.business.workflow.bean.param.ListParam;
import com.koron.common.core.business.workflow.bean.param.SubmitterParams;
import com.koron.common.core.business.workflow.bean.po.SysWorkflow;
import com.koron.common.core.business.workflow.bean.result.FlowList;
import com.koron.common.core.business.workflow.bean.result.TaskStatistics;
import com.koron.common.core.business.workflow.bean.result.UserResult;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工作流表(SysWorkflow表)控制层
 *
 * @author jzp
 * @since 2021-04-06 08:53:04
 */
@RestController
@RequestMapping("sys/Workflow")
@Api(tags = "工作流接口")
public class SysWorkflowController {
    private static final Logger logger = LoggerFactory.getLogger(SysWorkflowController.class);

    private final SysWorkflowService sysWorkflowService;

    public SysWorkflowController(SysWorkflowService sysWorkflowService) {
        this.sysWorkflowService = sysWorkflowService;
    }

    @GetMapping("view")
    @ApiOperation("获取工作流操作界面")
    public Response<String> workflowHTML(@RequestParam(value = "findId", required = false) @ApiParam(value = "id") String findId,
                                    @RequestParam(required = false) String processCode,
                                    @RequestParam(required = false) String variable
                                    ) {
        String workflowHTML = sysWorkflowService.queryWorkflowHTML(findId, processCode, variable);
        return Response.success(workflowHTML);
    }

    @GetMapping("viewOnly")
    @ApiOperation(value = "获取工作流操作界面，没有提交按钮", notes = "没有提交按钮")
    public Response<String> viewOnly(@RequestParam(value = "findId", required = false) @ApiParam(value = "id") String findId,
                                @RequestParam(required = false) String processCode,
                                @RequestParam(required = false) String variable
                                ) {
        String workflowHTML = sysWorkflowService.queryOnlyWorkflowHTML(findId, processCode, variable);
        return Response.success(workflowHTML);
    }

    @GetMapping("preview")
    @ApiOperation(value = "预览工作流操作界面", notes = "一般给草稿状态使用，返回null表示没有工作流")
    public Response<String> preview(@RequestParam(value = "findId") @ApiParam(value = "id") String findId,
                               @RequestParam(required = false) String variable) {
        String workflowHTML = sysWorkflowService.queryOnlyWorkflowHTML(findId, null, variable);
        if ("工作流不存在".equals(workflowHTML)) {
            workflowHTML = null;
        }
        return Response.success(workflowHTML);
    }

    @GetMapping("mobileView")
    @ApiOperation("获取移动端工作流操作界面")
    public Response<String> workflowMobileHTML(@RequestParam(value = "findId", required = false) @ApiParam(value = "id") String findId,
                                          @RequestParam(required = false) String processCode) {
        String workflowHTML = sysWorkflowService.queryWorkflowMobileHTML(findId, processCode);
        return Response.success(workflowHTML);
    }

    @GetMapping("mobileHistory")
    @ApiOperation("获取移动端工作流历史轨迹")
    public Response<Map> workflowMobileHistory(@RequestParam("findId") @ApiParam(value = "id") String findId) {
        String history = sysWorkflowService.queryWorkflowMobileHistory(findId);
        SysWorkflow workflow = sysWorkflowService.queryOneByDataId(findId);
        Map<String, Object> response = new HashMap<>();
        response.put("workflow", workflow);
        response.put("history", JSONArray.parseArray(history));
        return Response.success(response);
    }

    @ApiOperation("工作流办理接口")
    @PostMapping("submit")
    public Response<Object> submit(@ApiIgnore @CasUser EamUser user, @RequestBody SubmitQuery submitQuery) {
        Response<Object> submitResponse = sysWorkflowService.submit(user, submitQuery);
        if (submitResponse != null) {
            return submitResponse;
        }
        return Response.ok("提交成功", null);
    }

    @ApiOperation("获取选人组件过滤后的处理人")
    @PostMapping("handler")
    public Response<List<WorkflowStaff>> handler(@RequestBody HandlerParams handlerParams) {
        List<WorkflowStaff> userResultList = sysWorkflowService.getHandler(handlerParams.getProcessCode(), handlerParams.getDataNode(), handlerParams.getFormVariables());
        return Response.success("查询成功", userResultList);
    }

    @ApiOperation(value = "获取自动过滤后的提交人", notes = "如果是人工决策，需要传入nextNodeCode")
    @PostMapping("submitter")
    public Response<List<WorkflowStaff>> submitter(@RequestBody SubmitterParams submitterParams) {
        List<WorkflowStaff> userResultList = sysWorkflowService.getSubmitter(submitterParams.getProcessCode(), submitterParams.getNodeCode(), submitterParams.getNextNodeCode(), submitterParams.getFormVariables());
        return Response.success("查询成功", userResultList);
    }

    @ApiOperation(value = "获取运行中的节点处理人信息")
    @GetMapping("runTaskCandidateInfo")
    public Response<List<UserResult>> runTaskCandidateInfo(String id) {
        List<UserResult> userResults = sysWorkflowService.runTaskCandidateInfo(id);
        return Response.success(userResults);
    }

    @ApiOperation(value = "获取工作流列表")
    @GetMapping("billList")
    public Response<Page<FlowList>> billList(ListParam listParam) {
        return Response.success(sysWorkflowService.listBill(listParam));
    }

    @ApiOperation(value = "获取代办数量统计")
    @GetMapping("listUserTaskStatistics")
    public Response<List<TaskStatistics>> listUserTaskStatistics(@RequestParam(value = "startDate", required = false) String startDate,
                                                                 @RequestParam(value = "endDate", required = false) String endDate,
                                                                 @RequestParam(value = "setCode", required = false) String setCode,
                                                                 @RequestParam(value = "accountList", required = true) List<String> accountList) {
        return Response.success(sysWorkflowService.listUserTaskStatistics(startDate, endDate, setCode, accountList));
    }

}
